<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>用户注册</p>
    </header>
    <div class="login-box" >
      <div style="padding: 50px 20px;background-color: white;border-radius: 5px;">
        <el-form ref="formRef" :rules="data.rules"  :model="data.form" style=" width: 350px " >
          <el-form-item prop="userId">
            <el-input  size="large" v-model="data.form.userId" placeholder="请输入账号" prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="userName">
            <el-input  size="large" v-model="data.form.userName" placeholder="请输入用户名" prefix-icon="UserFilled"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input  show-password size="large" v-model="data.form.password" placeholder="请输入密码" prefix-icon="Lock"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input  show-password size="large" v-model="data.form.confirmPassword" placeholder="请确认密码" prefix-icon="Lock"></el-input>
          </el-form-item>
          <el-form-item prop="userSex">
            <el-select  v-model="data.form.userSex" style="width: 100% " size="large"  placeholder="请选择性别">
              <el-option value="1" label="男">  </el-option>
              <el-option value="0" label="女">  </el-option>
            </el-select>
          </el-form-item>
          <div style="margin-bottom: 20px">
            <el-button @click="register" size="large" style="width: 100%;color: #0742b1;">注册</el-button>
          </div>
          <div style="text-align:right;margin-right:2px">已有帐号？请 <a  style="text-decoration: none"href="/login">登陆</a></div>
        </el-form>
      </div>

    </div>


    <!-- 底部菜单部分 -->
    <Footer></Footer>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue';
import Footer from '../components/Footer.vue';
import { useRouter } from 'vue-router';
import request from "@/utils/request";
import { ElMessage } from "element-plus";


const validatePass = (rule, value, callback) => {
  if (!value) {
    callback(new Error('请再次确认密码'))
  } else if (value !== data.form.password) {
    callback(new Error("两次输入的密码不一致"))
  } else {
    callback()
  }
}

const data =reactive(
    {
      form:{},
      rules:{
        account:[
          {required:true,message:'请输入账号',trigger:'blur'},
          {min: 1, max: 16, message: '账号长度应在 1 到 16 个字符之间', trigger: 'blur' }
        ],
        adminName:[
          {required:true,message:'请输入用户名',trigger:'blur'},
          {min: 2, max: 16, message: '用户名长度应在 2 到 16 个字符之间', trigger: 'blur' }
        ],
        password:[
          {required:true,message:'请输入密码',trigger:'blur'},
          { min: 3, max: 8, message: '密码长度应在 3 到 8 个字符之间', trigger: 'blur' }
        ],
        confirmPassword:[
          {validator:validatePass,trigger:'blur'}
        ],
        userSex:[{ required: true, message: '请选择性别', trigger: 'change' }]


      }

    }
)
const formRef = ref()


const  register =()=>{
  formRef.value.validate((valid)=>{
    if(valid){
      const user = {
        userId: data.form.userId,
        password:data.form.password,
        userName:data.form.userName,
        userSex:data.form.userSex,
      }
      request.post('/User/saveUser',user).then(res=>{
        if(res.code === 1){
          //注册成功
          ElMessage.success("注册成功")
          location.href = '/login'
        }else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}

</script>

<style scoped>
/****************** 总容器 ******************/
.wrapper {
  width: 100%;
  height: 100%;
}
/****************** header部分 ******************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #17BAF9;
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
}
/****************** 表单部分 ******************/
.wrapper .form-box {
  width: 100%;
  margin-top: 12vw;
}
.wrapper .form-box li {
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
  display: flex;
  align-items: center;
}
.wrapper .form-box li .title {
  flex: 0 0 18vw;
  font-size: 3vw;
  font-weight: 700;
  color: #666;
}
.wrapper .form-box li .content {
  flex: 1;
}
.wrapper .form-box li .content input {
  border: none;
  outline: none;
  width: 100%;
  height: 4vw;
  font-size: 3vw;
}
.wrapper .button-login {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
}
.wrapper .button-login button {
  width: 100%;
  height: 10vw;
  font-size: 3.8vw;
  font-weight: 700;
  color: #fff;
  background-color: #38CA73;
  border-radius: 4px;
  border: none;
  outline: none;
}
.wrapper .button-register {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
}
.wrapper .button-register button {
  width: 100%;
  height: 10vw;
  font-size: 3.8vw;
  font-weight: 700;
  color: #666;
  background-color: #EEE;
  border-radius: 4px;
  border: none;
  outline: none;
  border: solid 1px #DDD;
}


.login-box{
  margin-top: 5vw;
  display: flex;
  align-items: center;
  width: 100%;
  height: 100%;
  bottom: 80px;


}
</style>